<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Prioritising Steps</title>
</head>

<body>

<h2>Prioritising Steps</h2>

<p>The matching of step candidate to textual steps is central to
JBehave's design. The first candidate that matches is used to
create an executable step. In some case, though, one can have cases in
which multiple candidates can match the same textual step, e.g.
when the steps are very similar.   Moreover, the list of candidates is derived
from the method signatures by invoking the <b>Class.getMethods()</b> method,
which may return the methods in a different order depending the the version of the JRE.</p>     

<span class="followup">Steps writers should never rely on the order in which the methods appear in the steps class.</span>

<p>For example, the step:</p>

<pre class="brush: bdd">
Then the value returned is empty
</pre>

<p>would be matched by both of the following:</p>

<pre class="brush: java">
    @Then("the value returned is empty")
    public void theValueIsEmpty()
    
    @Then("the value returned is $value")
    public void theValueIs(String value)
</pre>

<p>To address this, JBehave's <a
	href="javadoc/core/org/jbehave/core/steps/StepFinder.html">StepFinder</a> allows for a customisable <a
	href="javadoc/core/org/jbehave/core/steps/StepFinder.PrioritisingStrategy.html">PrioritisingStrategy</a>.</p>
<p>
The default strategy is <a
	href="javadoc/core/org/jbehave/core/steps/StepFinder.ByPriorityField.html">ByPriorityField</a>
which prioritises the steps by the priority field explicitly set in the annotations (the default priority is 0).
To prioritise one candidate over another simply set a non-zero positive priority:</p>

<pre class="brush: java">
    @Then(value="the value returned is empty", priority=1)
    public void theValueIsEmpty()
    
    @Then("the value returned is $value")
    public void theValueIs(String value)
</pre>

<p>An alternative strategy is the <a
	href="javadoc/core/org/jbehave/core/steps/StepFinder.ByLevenshteinDistance.html">ByLevenshteinDistance</a>
which prioritises the steps according to the <a
	href="http://en.wikipedia.org/wiki/Levenshtein_distance">Levenshtein Distance</a>.</p>

<p>To specify a custom strategy, simply create a StepFinder with the
desired strategy, and override the default step collector in the
configuration:</p>

<pre class="brush: java">
   StepCollector stepCollector = new MarkUnmatchedStepsAsPending(new StepFinder(new ByLevenshteinDistance()));
   new MostUsefulConfiguration().useStepCollector(stepCollector);
</pre>

<div class="clear">
<hr />
</div>

</body>
</html>
